package com.onlyyou.order.controller;

import com.onlyyou.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 小野
 * @Date: 2020/1/11 11:40
 * 商品接口
 */
@RestController
@Api(tags = "订单相关接口")
@RequestMapping("/")
@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService iOrderService;

    @PostMapping("/saveOrder")
    @ApiOperation(value = "添加订单")
    public Map<String, Object> saveOrder(String sku_id, Long user_id,Integer num) {
        return iOrderService.saveOrder(sku_id,user_id,num);
    }

    @RequestMapping(value = "/createOrder")
    public Map<String, Object> createOrder(String sku_id, Long user_id) {
//        HttpSession httpSession = httpServletRequest.getSession();
//        Object o = httpSession.getAttribute("user");
//        Map<String, Object> userMap = JSONObject.parseObject(o.toString(), Map.class);
        String strUser_id = new String(String.valueOf(user_id));
        return iOrderService.createOrder(sku_id, strUser_id); //userMap.get("user_id").toString()
    }

    @RequestMapping(value = "/getOrder/{order_id}")
    public Map<String, Object> getOrder(@PathVariable("order_id") String order_id) {
        return iOrderService.getOrder(order_id);
    }


    @RequestMapping(value = "/payOrder/{order_id}/{sku_id}")
    public Map<String, Object> payOrder(@PathVariable("order_id") String order_id, @PathVariable("sku_id") String sku_id) {
        //正常情况下在这里会调用支付接口，我们这里模拟支付已经返回正常数据
        boolean isPay = true;
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (!isPay) {
            resultMap.put("result", false);
            resultMap.put("msg", "支付接口调用失败！");
            return resultMap;
        }

        return iOrderService.payOrder(order_id, sku_id);
    }

    @GetMapping("/findAll")
    public Map<String, Object> findAll(Integer page, Integer limit) {
        return iOrderService.findAll(page, limit);
    }
}
